package com.demo.demos.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.demos.entity.User;
import com.demo.demos.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * Created by Lenovo
 * DATA:2025/2/28 028
 **/
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 分页获取所有的用户数据
     * @param pageNo 当前页
     * @param pageSize 页大小
     * @param name 永辉名称
     * @return
     */
    @GetMapping("page")
    public Page<User> page(
            @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
            @RequestParam("name")String name) {
       return userService.pageSearch(pageNo, pageSize,name);
    }


    /**
     * 根据用户的id获取用户信息
     * @param id 用户id
     * @return
     */
    @GetMapping("/")
    public User queryById(@RequestParam("id") Integer id) {
        System.out.println("id = " + id);
        return userService.getById(id);
    }

    /**
     * 新增用户信息
     * @param user 用户数据
     * @return
     */
    @PostMapping("add")
    public Boolean add(@RequestBody User user) {
        return userService.save(user);
    }

    /**
     * 根据用户id跟新用户的基本信息
     * @param user
     * @return
     */
    @PostMapping("update")
    public Boolean update(@RequestBody User user) {
        return userService.updateById(user);
    }

    /**
     * 更具用户id删除用户信息
     * @param id 用户id
     * @return
     */
    @DeleteMapping("delete")
    public Boolean delete(@RequestParam("id") Integer id) {
        return userService.removeById(id);
    }

}
